package com.asa.floatview

import android.app.Service
import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import android.widget.TextView

/**
 * create By：anderson
 * on 2022/5/2
 * desc:
 */
class FloatViewManager {


    companion object{

        val instance : FloatViewManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){FloatViewManager()}

    }

    private var  windowManager : WindowManager ? = null
    private var screenWidth:Int = 0
    private var screenHeight: Int = 0
    private var defaultX = 0
    private var defaultY = 0
    private var windowLayoutParams:WindowManager.LayoutParams ?= null
    private var floatingView: View?=null
    private var floatButton :TextView?=null
    private var rootView2:View?=null


    fun showWindow(context:Context){
        windowManager = context.getSystemService(Service.WINDOW_SERVICE) as WindowManager
        val defaultDisplay = windowManager!!.defaultDisplay
        val displayMetrics = DisplayMetrics()
        defaultDisplay.getMetrics(displayMetrics)
        screenWidth = displayMetrics.widthPixels
        screenHeight = displayMetrics.heightPixels
        //设置layoutParams
        windowLayoutParams = WindowManager.LayoutParams().apply {
            if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
                type =WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else{
                type = WindowManager.LayoutParams.TYPE_PHONE
            }
            format = PixelFormat.RGBA_8888
            flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            width = WindowManager.LayoutParams.WRAP_CONTENT
            height = WindowManager.LayoutParams.WRAP_CONTENT
            defaultX = screenWidth
            defaultY = screenHeight / 2 - screenHeight / 2 / 2
            x = defaultX
            y = defaultY
        }

        floatingView = LayoutInflater.from(context).inflate(R.layout.float_layout,null).apply {
//            rootView2 = findViewById(R.id.cl_root)
            floatButton = findViewById(R.id.tv_float)

        }  

        floatButton?.setOnClickListener {
            Log.e("日志", "click: ")
        }

        floatingView?.setOnTouchListener(onTouchListener)
        windowManager?.addView(floatingView,windowLayoutParams)
    }


    fun close(){
        windowManager?.removeView(floatingView)
    }

    private val onTouchListener = object :View.OnTouchListener{
        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            Log.e("日志", "onTouch: fsdfds")
            event?.also {
                val action = it.action
                if (action == MotionEvent.ACTION_DOWN){
                    Log.e("日志", "down: x:${it.x}  y:${it.y}")
                } else if (action == MotionEvent.ACTION_MOVE){
                    Log.e("日志", "move: x:${it.x}  y:${it.y}")
                }

            }
            return true
        }
    }

}